package com.peycash.reverso.endpoint;

import java.io.IOException;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOMsg;
import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;

import com.peycash.reverso.dto.R001;
import com.peycash.reverso.dto.T001;

public class ProcesadorReversoEndPoint  extends AbstractJDomPayloadEndpoint {

	private PeyCashISOUtilsService isoutils;
	
	@Override
	protected Element invokeInternal(Element requestElement) throws Exception {
		Element result = procesa(requestElement);
		return result;
	}
	
	private Element procesa(Element root) {
        T001 t001 = Translator.translate(root.getChildren());
        ISOMsg m = null;
        ISOMsg r = null;
        R001 r001 = null;
        try {
                m = isoutils.translateReverso(t001);
                r = isoutils.sendMessage(m);
                r001 = isoutils.translateMessage(r);
        } catch (ISOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }

        /* processing t001 */
        if (r001 == null) {
                r001 = new R001();
                r001.setR1("00");
                r001.setR3("00");
                r001.setR4("00");
                r001.setR5("00");
                r001.setR6("00");
                r001.setR7("00");
                r001.setR8("00");
                r001.setR9("00");
                r001.setR10("00");
        }
        Element element = crearResponse(r001);
        return element;
}

	/** 
	 * A partir de un objeto de tipo R001 (Respuesta de Transacci&oacute;n), genera un objeto de tipo Element que ser&aacute; la respuesta del servicio web
	 * @param r001 Objeto cuyas propiedades corresponden a la trama R001 (Respuesta de Transacci&oacute;n).
	 * @return Objeto de tipo Element que ser&aacute; la respuesta del servicio web.
	 */
	private Element crearResponse(R001 r001) {
        Namespace xml = Namespace.getNamespace("xhtml", "http://187.174.207.114:8080/reverso/schemas");		
		Element elmRoot = new Element("PeyCashReversoResponse", xml);
		elmRoot.addContent(new Element("r1", xml).setText(r001.getR1()));
		elmRoot.addContent(new Element("r2", xml).setText(r001.getR2()));
		elmRoot.addContent(new Element("r3", xml).setText(r001.getR3()));
		elmRoot.addContent(new Element("r4", xml).setText(r001.getR4()));
		elmRoot.addContent(new Element("r5", xml).setText(r001.getR5()));
		elmRoot.addContent(new Element("r6", xml).setText(r001.getR6()));
		elmRoot.addContent(new Element("r7", xml).setText(r001.getR7()));
		elmRoot.addContent(new Element("r8", xml).setText(r001.getR8()));
		elmRoot.addContent(new Element("r9", xml).setText(r001.getR9()));
		elmRoot.addContent(new Element("r10", xml).setText(r001.getR10()));
		Document doc = new Document(elmRoot); 
        Element elemntDoc = doc.getRootElement();
		return elemntDoc;
	}

	/**
	 * @return the isoutils
	 */
	public PeyCashISOUtilsService getIsoutils() {
		return isoutils;
	}

	/**
	 * @param isoutils the isoutils to set
	 */
	public void setIsoutils(PeyCashISOUtilsService isoutils) {
		this.isoutils = isoutils;
	}

	
}
